package com.dkm.biz.gl.domain;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.date.DateConverter;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.constants.eav.DataType;
import com.dkm.biz.gl.persistence.dao.EavValueDao;
import com.dkm.biz.gl.persistence.entity.EavValuePo;
import com.dkm.biz.gl.repository.GlTextRepository;

/**
 * 对象功能:c6_eav_value 领域对象实体
 * 开发公司:唯心六艺软件 - Craft6.cn
 * 开发人员:winston
 * 创建时间:2014-07-30 15:50:12
 */
@Service
@Scope("prototype")
public class EavValue extends AbstractDomain<String, EavValuePo>{
	 
	 private EavValueDao eavValueDao = null;
	 
	 @Resource
	 private GlTextRepository glTextRepository;

	protected void init(){
		eavValueDao = SpringHelper.getBean(EavValueDao.class);
		setDao(eavValueDao);
	}	 
	 
	public void setValue(String dataType_,String value_){
		DataType dataType = DataType.valueOf(dataType_);
		switch (dataType) {
		case INT:
			getData().setLongVal(Long.valueOf(value_));			
			break;
		case DECIMAL:
			getData().setDoubleVal(Double.valueOf(value_));			
			break;
		case VARCHAR:
			getData().setStringVal(value_);
			break;
		case DATETIME:
			try {
				getData().setDatetimeVal(DateConverter.toDate(value_));	
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case TEXT:
			String textId = getData().getTextId();
			boolean isSaved = false;
			if(StringUtils.isNotEmpty(textId)){
				GlText glText = glTextRepository.load(textId);
				if(glText!=null){
					glText.getData().setText(value_);
					glText.save();
					isSaved = true;
				}
			}					
			if(!isSaved){
				GlText glText = glTextRepository.newInstance();
				glText.getData().setText(value_);
				glText.save();
				getData().setTextId(glText.getId());
			}
			break;			
		default:
			break;
		}
	}
}
